public class Solution151 {
    public String reverseWords(String s) {
        String[] strings=s.split(" ");
        for (int l=0,r=strings.length-1;l<r;l++,r--){
            while (l<r && strings[l].length()==0){
                l++;
            }
            while (l<r && strings[r].length()==0){
                r--;
            }
            String ss=strings[l];
            strings[l]=strings[r];
            strings[r]=ss;
        }
        StringBuilder stringBuilder=new StringBuilder();
        for (String string : strings) {
            if (string.length()!=0) {
                stringBuilder.append(string).append(' ');
            }
        }
        return stringBuilder.deleteCharAt(stringBuilder.length()-1).toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution151().reverseWords("a good   example"));
    }
}
